using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    public class clsTRANG_THAI_VTPT
    {
        public clsTRANG_THAI_VTPT(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }

        #region Private methods
        private TRANG_THAI_VTPT GetDataRow(DataRow row)
        {
            TRANG_THAI_VTPT objTRANG_THAI_VTPT = new TRANG_THAI_VTPT();
            try
            {
                if (row["MaTrangThai"] != DBNull.Value)
                    objTRANG_THAI_VTPT.MaTrangThai = int.Parse(row["MaTrangThai"].ToString());
            }
            catch { }
            try
            {
                if (row["TenTrangThai"] != DBNull.Value)
                    objTRANG_THAI_VTPT.TenTrangThai = ((row["TenTrangThai"]).ToString());
            }
            catch { }

            return objTRANG_THAI_VTPT;
        }
        private List<TRANG_THAI_VTPT> GetTRANG_THAI_VTPT(DataTable dt)
        {
            List<TRANG_THAI_VTPT> lst = new List<TRANG_THAI_VTPT>();
            TRANG_THAI_VTPT obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = this.GetDataRow(dt.Rows[i]);              
                lst.Add(obj);
            }
            return lst;
        }
        #endregion Private methods

        #region Public methods

        public TRANG_THAI_VTPT usp_SelectTRANG_THAI_VTPTByMaTrangThai(int matrangthai)
        {
            string spName = "select * from TRANG_THAI_VTPT where matrangthai="+matrangthai.ToString();
            DataTable dt = new DataTable();
            List<TRANG_THAI_VTPT> lst = new List<TRANG_THAI_VTPT>();
            try
            {
                SqlHelper.OpenConnection();

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName,CommandType.Text);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, CommandType.Text);
                }
                lst = this.GetTRANG_THAI_VTPT(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            if (lst.Count == 0)
                return null;
            return lst[0];
        }

        public List<TRANG_THAI_VTPT> usp_SelectTRANG_THAI_VTPTsAll()
        {
            string spName = "usp_SelectTRANG_THAI_VTPTsAll";
            DataTable dt = new DataTable();
            List<TRANG_THAI_VTPT> lst = new List<TRANG_THAI_VTPT>();
            try
            {
                SqlHelper.OpenConnection();

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, null);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, null);
                }
                lst = this.GetTRANG_THAI_VTPT(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }
        public TRANG_THAI_VTPT usp_SelectTRANG_THAI_VTPTByItemCategory(string iCate)
        {
            string spName = "select * from TRANG_THAI_VTPT where ItemCategory='"+iCate+"'";
            DataTable dt = new DataTable();
            List<TRANG_THAI_VTPT> lst = new List<TRANG_THAI_VTPT>();
            try
            {
                SqlHelper.OpenConnection();

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, CommandType.Text);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, CommandType.Text);
                }
                lst = this.GetTRANG_THAI_VTPT(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            if (lst.Count > 0)
                return lst[0];
            else
                return null;
        }

        public List<TRANG_THAI_VTPT> usp_SelectTRANG_THAI_VTPTByMaVTPT_TON_KHO(string MaVTPT, string MaKho)
        {
            string spName = "usp_SelectTRANG_THAI_VTPTByMaVTPT_TON_KHO";
            DataTable dt = new DataTable();
            List<TRANG_THAI_VTPT> lst = new List<TRANG_THAI_VTPT>();
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@MaVTPT", MaVTPT);
                par[1] = new SqlParameter("@MaKho", MaKho);

                dt = SqlHelper.ExecuteToDataTable(spName, par);

                lst = this.GetTRANG_THAI_VTPT(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }
        #endregion
    }
}
